//package com.registration.Aspect;
//
//import cn.dev33.satoken.stp.StpUtil;
//import com.registration.Annotation.OperateLog;
//import com.registration.bean.LogBean;
//import com.registration.bean.PCUserBean;
//import com.registration.bean.UserBean;
//import com.registration.mapper.LogMapper;
//import com.registration.mapper.PCUserMapper;
//import com.registration.mapper.UserMapper;
//import com.registration.util.JsonResponse;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.Date;
//
///**
// * 操作日志 AOP 切面
// */
//@Aspect // 声明为切面类
//@Component // 交给 Spring 管理
//@Order(2)
//public class OperateLogAspect {
//
//    @Autowired
//    private LogMapper logMapper;
//
//    @Autowired
//    private PCUserMapper pcUserMapper;
//
//    @Autowired
//    private UserMapper userMapper;
//
//    /**
//     * 1. 定义切入点：拦截所有 Controller 方法
//     */
//    @Pointcut("execution(* com.registration.controller..*(..)) ")
//    public void operateLogPointcut() {
//    }
//
//    @Around("operateLogPointcut()")
//    public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
//
//        /*System.out.println("======= OperateLogAspect 被触发 =======");
//        System.out.println("Sa-Token信息: " + StpUtil.getTokenInfo());
//        System.out.println("是否已登录: " + StpUtil.isLogin());
//        */
//        LogBean log = new LogBean();
//        Object loginId = StpUtil.getLoginIdDefaultNull();
//
//        /*System.out.println("loginId: " + loginId);
//        System.out.println("loginId类型: " + (loginId != null ? loginId.getClass().getName() : "null"));
//        System.out.println("loginType: " + loginType);*/
//
//
//        String methodName = null;
//
//        Class targClass = joinPoint.getTarget().getClass();
//        Method[] methods = targClass.getDeclaredMethods();
//        for (Method method : methods) {
//            if (method.getName().equals(joinPoint.getSignature().getName())) {
//                OperateLog operateLog = method.getAnnotation(OperateLog.class);
//                if (operateLog != null) {
//                    methodName = operateLog.value();
//                    break;
//                }else {
//                    methodName = "未定义";
//                }
//            }
//        }
//
//
//       /* System.out.println("被处理的对象类名" + joinPoint.getTarget().getClass().getName());
//        System.out.println("被处理的对象方法名" + joinPoint.getSignature().getName());
//        System.out.println("目标对象的参数" + Arrays.toString(joinPoint.getArgs()));
//        System.out.println("loginId类型: " + (loginId != null ? loginId.getClass().getName() : "null"));
//        */
//        try {
//            String loginIdStr = loginId != null ? loginId.toString() : "null";
//            if (loginId != null) {
//
//                if ( loginIdStr.matches(".*[a-zA-Z].*")) {
//                    PCUserBean user = pcUserMapper.login((String) loginId);
//                    log.setPc_user_id(user.getPc_user_id());
//                    if (user != null) {
//                        log.setReal_name(user.getReal_name());
//                    } else {
//                        log.setReal_name("未知管理员");
//                    }
//                } else {
//                    UserBean userBean = userMapper.selectUserByPhone((String) loginId);
//                    if (userBean != null) {
//                        log.setPc_user_id(userBean.getUser_id());
//                        log.setReal_name(userBean.getUsername());
//                    } else {
//                        log.setReal_name("未知用户");
//                    }
//                }
//            } else {
//                log.setReal_name("未登录用户");
//            }
//
//            Object result = joinPoint.proceed();
//            if (result instanceof JsonResponse response) {
//                log.setStatus(response.getCode());
//                log.setLog_content(response.getMsg());
//            }
//            log.setBusiness(methodName);
//            log.setMethod(joinPoint.getSignature().getName());
//            log.setCreate_time(new Date());
//            logMapper.addLog(log);
//            /*System.out.println("======= OperateLogAspect 被触发 =======");
//            System.out.println("Sa-Token信息: " + StpUtil.getTokenInfo());
//            System.out.println("是否已登录: " + StpUtil.isLogin());
//            System.out.println("loginId: " + loginId);
//            System.out.println("loginId类型: " + (loginId != null ? loginId.getClass().getName() : "null"));
//            */
//
//            cleanupOldLogs();
//            return result; // 正常返回结果
//
//        } catch (Throwable throwable) {
//            // 记录异常日志
//            System.out.println("出现异常");
//            log.setStatus(0);
//            log.setLog_content("执行异常: " + throwable.getMessage());
//            logMapper.addLog(log);
//            // 重新抛出异常
//            throw throwable;
//        }
//
//    }
//
//    private void cleanupOldLogs() {
//        try {
//            int totalCount = logMapper.countLogs();
//            if (totalCount > 300) {
//                logMapper.deleteOldestLogs(50);
//
//                LogBean logBean=new LogBean(null,"cleanupOldLogs", "清理日志",0 , "系统", 0, "日志超过300条自动删除前50条", new Date());
//                logMapper.addLog(logBean);
//
//            }
//        } catch (Exception e) {
//            System.out.println("清理旧日志时出错: " + e.getMessage());
//            // 不中断主流程，仅记录错误
//        }
//    }
//}
